<?php
/*
Plugin Name: WP HUKD
Plugin URI: http://code.google.com/p/wp-hukd/
Description: A widget for accessing the latest deals on HUKDs.
Version: 0.01
Author: Robin Card
Author URI: http://code.google.com/p/wp-hukd/
License: GPL2
*/
$api_key=get_option('api_key'); // feel free to replace this with your API key but it will disable the ability to edit the API key via the wordpress back-office


error_reporting(E_ALL);
define('HUKD_API_KEY',$api_key);


define('API_URI','http://api.hotukdeals.com/rest_api/v2/?key=');
define('CACHE_PATH','wp-content/plugins/wp-hukd/cache/');

register_activation_hook( __FILE__, array('hukd_widget','activate'));
register_deactivation_hook( __FILE__, array('hukd_widget','deactivate'));

function hukd_test_key($key='',$cache=true)
{
	if($key)
	{
		$key_to_test=$key;
	}
	else
	{
		$key_to_test=HUKD_API_KEY;
	}
	
	if($cache)
	{
		$response=hukd_cache_file(API_URI.$key_to_test,604800);
	}
	else
	{
		$response=hukd_cache_file(API_URI.$key_to_test,1);	
	}
	return hukd_parse_response($response)!=false;	
}


function hukd_get_forums()
{
	$forums=array();
	
	$forums['All']='all';
	$forums['Deals']='deals';
	$forums['Vouchers']='vouchers';
	$forums['Freebies']='freebies';
	$forums['Competitions']='competitions';
	$forums['Deal Requests']='deal-requests';
	$forums['For Sale/Trade']='for-sale-trade';
	$forums['Misc']='misc';
	$forums['Feedback']='feedback';
	
	
	return $forums;
	
}


function hukd_check_valid_forum($name)
{

	$forums=hukd_get_forums();
	
	if(in_array($name,$forums))
	{
		return true;
	}
	else
	{
		return false;
	}
	
}

function hukd_get_categories()
{
	$categories=array();
	$categories['All']='all';
	$categories['Clothing']='clothing';
	$categories['Computers']='computers';
	$categories['Electricals']='electricals';
	$categories['Entertainment']='entertainment';
	$categories['Finance']='finance';
	$categories['General Deals']='general-deals';
	$categories['Groceries']='groceries';
	$categories['Home']='home';
	$categories['Kids']='kids';
	$categories['Mobiles']='mobiles';
	$categories['Nonsense']='nonsense';
	$categories['Travel']='travel';
	
	return $categories;
}

function hukd_check_valid_category($name)
{

	$categories=hukd_get_categories();
	
	if(in_array($name,$categories))
	{
		return true;
	}
	else
	{
		return false;
	}
	
}

function hukd_check_valid_online_offline($name)
{
	$online_offline=explode(',','online,offline');
	
	if(in_array($name,$online_offline))
	{
		return true;
	}
	else
	{
		return false;
	}
	


}


function hukd_get_orders()
{
	$orders=array();
	$orders['Newest']='new';
	$orders['Most Discussed']='discussed';
	$orders['Heat']='hot';
	return $orders;
}

function hukd_check_valid_order($order)
{

	$orders=hukd_get_orders();
	
	if(in_array($order,$orders))
	{
		return true;
	}
	else
	{
		return false;
	}


}

function hukd_check_valid_exclude_expired($exlude_expired)
{
	

	$exlude_expireds=explode(',','true,false');
	
	if(in_array($exlude_expired,$exlude_expireds))
	{
		return true;
	}
	else
	{
		return false;
	}


}



function hukd_check_valid_paramater($paramater_name,$paramater_value='')
{
	
		  					$paramaters=explode(',','forum,category,merchant,tag,username,online_offline,order,page,results_per_page,min_temp,exclude_expired,search');
		
		if(!in_array($paramater_name,$paramaters))
		{
			hukd_error('invalid parameter name');
			return false;
		
		}  
		
		if(!$paramater_value)
		{
			return true;
		}
		
		if($paramater_name=='forum')
		{
				if(hukd_check_valid_forum($paramater_value))
				{
					return true;
				}
				else
				{			
					hukd_error("'$paramater_value' is not a valid forum name");	
					return false;
				}
		}
		
		if($paramater_name=='category')
		{
				if(hukd_check_valid_category($paramater_value))
				{
					return true;
				}
				else
				{			
					hukd_error("'$paramater_value' is not a valid category name");	
					return false;
				}
		}
		
		if($paramater_name=='merchant')
		{
				return true;
		}
		
		if($paramater_name=='username')
		{
				return true;
		}
		
		if($paramater_name=="online_offline")
		{
			
			if(hukd_check_valid_online_offline($paramater_value))
				{
					return true;
				}
				else
				{			
					hukd_error("'$paramater_value' is not a online_offline value, only 'online' or 'offline' are valid");	
					return false;
				}
		
				
		}
		
		if($paramater_name=="order")
		{
			
			if(hukd_check_valid_order($paramater_value))
				{
					return true;
				}
				else
				{			
					hukd_error("'$paramater_value' is not a valid order, only new, discussed and hot are value.");	
					return false;
				}
		
				
		}
		

		if($paramater_name=='page')
		{
			
			if(floatval($paramater_value)==intval($paramater_value) && abs(intval($paramater_value))==$paramater_value )
			{
				return true;
			}
			else
			{
				hukd_error("'$paramater_value' is not a valid value for page, must be a whole number .");	
				return false;
			}
			
		}		
		
		
		
		if($paramater_name=='results_per_page')
		{
			if(is_int(intval($paramater_value)))
			{
				return true;
			}
			else
			{
				hukd_error("'$paramater_value' is not a valid value for results per page, must be a whole number .");	
				return false;
			}
			
		}
		
		if($paramater_name=='min_temp')
		{
			if(is_numeric(($paramater_value)))
			{
				return true;
			}
			else
			{
				hukd_error("'$paramater_value' is not a valid value for min_temp, must be a number.");	
				return false;
			}
			
		}	
		
		
		
		if($paramater_name=='exclude_expired')
		{
			//not is_bool() because exclude is a string value of true, false
			
			if(hukd_check_valid_exclude_expired($paramater_value))
			{
				return true;
			}
			else
			{
				hukd_error("'$paramater_value' is not a valid value for exclude_expired, must be string, 'true' or 'false'.");	
				return false;
			}
			
		}		
		if($paramater_name=="search")
		{
			
			return true;
		}
}

function hukd_parse_response($xml_string)
{
	$xml = new SimplexmlElement($xml_string);
	
	if(property_exists($xml,'error'))
	{
		hukd_error($xml->error);
		return false;	
	}
	
	return $xml;
}



function hukd_error($error_message)
{
	trigger_error('(HUKD) '.$error_message,E_USER_WARNING);
}



function hukd_fetch(array $parameters=array())
{
	
	if(count($parameters)>0)
	{
		$allowed=true;
		foreach($parameters as $paramater_name => $paramater_value)
		{
			if(!hukd_check_valid_paramater($paramater_name,$paramater_value))
			{
				$allowed=true;
			}
			
		}
		if($allowed==false)
		{
			return false;
		}
		
		$query_string=http_build_query($parameters);
	}
	
	return (hukd_parse_response(hukd_cache_file(API_URI.HUKD_API_KEY.'&'.$query_string))->deals->api_item);
	

}

 

// caches remotely fetched files. Returns string;
function hukd_cache_file($url,$stay_fresh=180)
{
	if(!is_writable(CACHE_PATH))
	{
		return file_get_contents($url);
	}
	$path=CACHE_PATH.md5($url);
	
	if(file_exists($path))
	{
		$last_saved=filemtime($path);
	}
	else
	{
		$last_saved=0;
	}
	
	if(!file_exists($path) || (time()-$last_saved)>$stay_fresh )
	{	
	
		$file=file_get_contents($url);
		
		if($file)
		{	
			file_put_contents($path,$file);
		}
	}
	
	
	return file_get_contents($path);
}

include('widget.php');


?>